capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B4
Covariate balance test
*/

local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;
local controls_no  "" ; 

use "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", clear ;

/* RD variables */
gen dobdd_run2=dobdd_run*dobdd_run;
gen dobdd_run3=dobdd_run2*dobdd_run;
gen dobdd_run4=dobdd_run3*dobdd_run;

gen postXdobdd=n_post*dobdd_run;
gen postXdobdd2=n_post*dobdd_run2;
gen postXdobdd3=n_post*dobdd_run3;
gen postXdobdd4=n_post*dobdd_run4;

local poly_1 "dobdd_run postXdobdd";
local poly_2 "dobdd_run dobdd_run2 postXdobdd postXdobdd2";
local poly_3 "dobdd_run dobdd_run2 dobdd_run3 postXdobdd postXdobdd2 postXdobdd3";
local poly_4 "dobdd_run dobdd_run2 dobdd_run3 dobdd_run4 postXdobdd postXdobdd2 postXdobdd3 postXdobdd4";

local bwidth_list "200 264 365" ;
local poly_max = 4 ;

matrix covbal = J(`poly_max',50,.) ;


forval poly = 1(1)`poly_max' {;
	
	local count_`poly' = 0 ;
	
	foreach cov in no { ;
		
		foreach bwidth in `bwidth_list' {;
					
			local count_`poly' = `count_`poly'' + 1 ;
		
			foreach var of varlist `covariates' {;		
				reg `var' n_post `poly_`poly'' `controls_`cov''
					if abs(dobdd_run)<=`bwidth' & keep_1997_2017_2c == 1 ; 
				estimates store est_`var';  /* Running up against character limit */
			} ;
			
			suest est_*, vce(robust) ;
			test n_post ;
			matrix covbal[`poly',`count_`poly''] = r(p) ;
			estimates drop est_* ;
			
		} ; /* end bwidth */
	} ; /* end cov */
} ; /* end poly */

clear ;
svmat covbal ;
save "`output'\\prwora_crime_covbal.dta", replace ;

capture log close;
